const app = getApp()
import {
    getBindCompanySupplier
} from '../../../../../api/app.js'
import {
    inputchange
} from '../../../../../utils/util.js'
import user from '../../../../../utils/user.js'
Component({
    data: {
        supplier: {
            name: '',
            id: '',
            phone: '',
            price: '',
            billingPolicy: '',
            priceUnit: '天'
        },
        supplierList: [],
        searchSupperlier: [],
        setPriceUnitStatus: false
    },
    properties: {
        projectId: String
    },
    observers: {},
    ready(){
        getBindCompanySupplier({
            companyId: user.companyId,
            type: 1
        }).then(res=>{
            this.setData({
                supplierList: res
            })
        })
    },
    methods: {
        searchSupplier(event){
            const result = []
            for(const i in this.data.supplierList){
                if(this.data.supplierList[i].contacts.indexOf(event.detail.value) !== -1){
                    result.push(this.data.supplierList[i])
                }
            }
            this.setData({
                ['searchSupperlier']: result,
                ['supplier.id']: '',
                ['supplier.name']: event.detail.value,
                showSearchStatus: !!result.length,
            })
        },
        setSupperName(event){
            this.setData({
                showSearchStatus: false,
                ['supplier.name']: event.currentTarget.dataset.name,
                ['supplier.phone']: event.currentTarget.dataset.phone,
                ['supplier.id']: event.currentTarget.dataset.id,
            })
			
        },
        // 手動輸入一個新的
        supplierConfirm(event){
            this.setData({
                showSearchStatus: false,
                ['supplier.name']: event.detail.value,
                ['supplier.id']: '',
            })
        },
        setWorkerSupplier(){
            if(!this.data.supplier.name){
                app.toast('请输入供应商名称')
                return 0
            }
            if(!/^1\d{10}$/.test(this.data.supplier.phone)){
                app.toast('请输入正确的手机号码')
                return 0
            }
            this.triggerEvent('submit',this.data.supplier)
        },
        inputchange(event) {
            inputchange(event, this)
        },
        close() {
            this.triggerEvent('cancel')
        },
        setPriceUnitStatus(){
            this.setData({
                'setPriceUnitStatus': !this.data.setPriceUnitStatus
            })
        },
        setPriceUnit(event){
            if(event.currentTarget.dataset.unit === '同工同酬'){
                this.setData({
                    'price': ''
                })
            }
            this.setData({
                'setPriceUnitStatus': false,
                'supplier.priceUnit': event.currentTarget.dataset.unit
            })
        },
    }

})
